<script lang="ts" setup>
import { defineOptions, ref, onMounted } from 'vue'
import monacoHeader from '@/components/monaco/monacoHeader.vue'
import monacoEditor from '@/components/monaco/monacoEditor.vue'
import { useChangeEditorConfig } from '@/hooks/useChangeEditorConfig'
import { useRunPyCode } from '@/hooks/useRunPyCode'
import cartoonFigure from '@/components/cartoonFigure/cartoonFigure.vue'
import { useDivDrag } from '@/hooks/useDivDrag'
// import { cartoonStatus } from '@/types/enum'
import type { ResultStatus as ResultStatusEnum } from '@/components/resultShow/resultShow.type'
import { ResultStatus } from '@/components/resultShow/resultShow.type'
defineOptions({
  name: 'pythonEditor'
})

const { code, getRunPythonCodeResults } = useRunPyCode()
const { handleThemeChange, editorOptions, handleFontSizeChange } = useChangeEditorConfig()

// =========== 实现编辑器的宽高自适应 START ================
const editorContainer = ref(null)
const editor = ref<HTMLDivElement>()
onMounted(() => {
  const resizeObserver = new ResizeObserver(() => {
    editor.value?.resizeEditor()
  })
  resizeObserver.observe(editorContainer.value)
})
// =========== 实现编辑器的宽高自适应 DONE ================

// =========== 实现切换编辑器的主题 =======================
const handleChangeEditorTheme = () => {
  handleThemeChange(editor)
}
const handleChangeEditorFontSize = () => {
  handleFontSizeChange(editor)
}
// =====================================================

// ========== 点击卡通按钮可以打开对话框 ===================
const isVisible = ref(false)
const handleClickCartoonFigure = () => {
  // 判断鼠标位移量
  if (dragFlag.value) return
  isVisible.value = !isVisible.value
}

// 可拖动卡通任务
const { dragBoxRef, dragFlag } = useDivDrag()

// 控制弹窗
const isDialogVisible = ref(false)
const runningResults = ref('')
const runngingStatus = ref<ResultStatusEnum>()

const callbackForStartRunPythonCode = () => {
  isDialogVisible.value = true
  runngingStatus.value = ResultStatus.isRunning
}

const callbackForRunPythonCodeResult = (res: any) => {
  const { success, result } = res.data
  runningResults.value = result
  if (success) {
    runngingStatus.value = ResultStatus.success
  } else {
    runngingStatus.value = ResultStatus.error
  }
}

const handleCheckResult = () => {
  isDialogVisible.value = true
}
</script>

<template>
  <div class="code-mode-container">
    <!-- 头部导航栏 -->
    <monaco-header :editor="editor">
      <template #left>
        <form-label>欢迎学习Python!૮(˶ᵔ ᵕ ᵔ˶)ა</form-label>
        <form-label label="主题">
          <el-select
            style="width: 100px"
            v-model="editorOptions.theme"
            placeholder="请选择主题"
            @change="handleChangeEditorTheme"
          >
            <el-option label="vs-dark" value="vs-dark"></el-option>
            <el-option label="vs-light" value="vs-light"></el-option>
          </el-select>
        </form-label>
        <form-label label="字号">
          <el-select
            style="width: 100px"
            v-model="editorOptions.fontSize"
            @change="handleChangeEditorFontSize"
          >
            <el-option
              v-for="item in editorOptions.fontSizeOptionsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </form-label>
      </template>
      <template #right>
        <el-button @click="handleCheckResult" type="success"
          ><el-icon style="margin-right: 10px"><View /></el-icon>运行结果</el-button
        >
        <el-button
          color="#874ccc"
          @click="
            getRunPythonCodeResults(callbackForStartRunPythonCode, callbackForRunPythonCodeResult)
          "
          ><el-icon style="margin-right: 10px"><Pointer /></el-icon>运行代码</el-button
        >
        <el-button @click="() => {}" type="danger">
          <el-icon style="margin-right: 10px"><Refresh /></el-icon>
          重置代码
        </el-button>
      </template>
    </monaco-header>
    <!-- 编辑器 -->
    <section class="code-mode-container__editor">
      <!-- monaco-editor -->
      <section class="code-mode-container__editor__workspace" ref="editorContainer">
        <monaco-editor ref="editor" v-model:code="code"></monaco-editor>
      </section>
      <!-- flow-chart -->
      <section class="code-mode-container__editor__preview">
        <flow-chart :code="code" :set-width="false"></flow-chart>
      </section>
    </section>
    <!-- 卡通形象 -->
    <section
      class="cartoon-figure__container"
      v-show="!isVisible && !isDialogVisible"
      ref="dragBoxRef"
    >
      <cartoon-figure @click="handleClickCartoonFigure"></cartoon-figure>
    </section>
    <!-- 对话框 -->
    <chat-dialog v-model:visible="isVisible" :width="'80vw'"></chat-dialog>
    <!-- 显示代码运行是否正确的弹窗 -->
    <result-show
      v-model="isDialogVisible"
      :runningResults="runningResults"
      :runngingStatus="runngingStatus"
    ></result-show>
  </div>
</template>

<style lang="scss" scoped>
.code-mode-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  flex-direction: column;
  overflow-y: auto;
  .form-label {
    color: var(--font-color);
  }
  .code-mode-container__editor {
    flex: 1;
    display: flex;
    .code-mode-container__editor__workspace,
    .code-mode-container__editor__preview {
      // width: 100%;
      position: relative;
      flex: 1;
      height: 100%;
      :deep(.el-text),
      :deep(.el-tag) {
        font-size: 20px;
      }
      :deep(.el-tag) {
        padding: 10px 15px;
        box-sizing: content-box;
        border-radius: 10px;
      }
    }
  }
  .cartoon-figure__container {
    position: fixed;
    max-width: 500px;
    max-height: 500px;
    right: -90px;
    bottom: -50px;
  }
}
.code-status-tips {
  position: absolute;
  text-align: center;
  top: 20px;
  width: 100%;
  margin-left: 50%;
  transform: translateX(-50%);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
